home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************
- *
- * tclogger.c: Program to help me log my time card time for
- * several different charge numbers.
- *
- * This program is distributed under the GNU General
- * Public License (GPL).
- * See http://www.gnu.org/copyleft/gpl.html
- *
- * This software comes with NO warranty at all and I
- * cannot be held responsible for anything it may do.
- * I make no claims as to my ability to write programs.
- * You have the source code, so check it out.
- *
- * This program was developed on a Linux/GNU system using FREE
- * tools. A special thanks to all programmers who developed
- * the following tools I used:
- *
- * pilot-xfer-0.9.0
- * gcc-2.7.2 cross compiler for Palm Connected Organizers
- * gdb-4.16
- * prctools-0.5.0
- * pilot-template-1.31
- * xcopilot-0.6.6
- *
- * Kevin Dupree
- * kdupree@flash.net
- *************************************************************/
-
- #pragma pack(2)
-
- #include <Common.h>
- #include <System/SysAll.h>
- #include <UI/UIAll.h>
-
- #include "callback.h"
- #include "tclogger.h"
- #include "util.h"
-
- /*************************************************************
- * Defines *
- *************************************************************/
- #define tcloggerCreator 'TCLg'
- /* Define the minimum OS version allowed */
- #define Version20 sysMakeROMVersion (2, 0, 0, 0, 0)
- #define Version30 sysMakeROMVersion (3, 0, 0, 0, 0)
- #define NUMTIMERS 7
-
- /*************************************************************
- * Type Definitions *
- *************************************************************/
-
- typedef struct {
- Boolean bRunning : 1;
- Long lStartTime;
- Long lAccumTime;
- Char sDescription[ 21 ];
- } TimerType;
-
- typedef enum
- {
- HOURS,
- HOURS_MIN,
- SECONDS
- } TimerFormatType;
-
- /* preferences state file. */
- typedef struct {
- TimerFormatType AccumFormat;
- Char sInTime[ 41 ];
- Char sOutTime[ 41 ];
- TimerType TimerState[ NUMTIMERS ];
- } TCLoggerPrefType;
-
-
- /*************************************************************
- * Global Variables *
- *************************************************************/
-
- static TimerType TimerData[ NUMTIMERS ];
- static VoidHand hndExport;
- static VoidHand hndInDesc;
- static VoidHand hndOutDesc;
- static VoidHand hndTime[ NUMTIMERS ];
- static VoidHand hndDesc[ NUMTIMERS ];
- static TimerFormatType AccumFormat = HOURS;
- static Char sInTime[ 41 ];
- static Char sOutTime[ 41 ];
- SystemPreferencesType sysPrefs; /* User's Pilot preferences. */
- static Boolean bOKBold;
- static VoidHand hndTotal;
-
-
- /*************************************************************
- * Routines *
- *************************************************************/
- static Word StartApplication( void );
- static void EventLoop( void );
- static Boolean ApplicationHandleEvent( EventPtr );
- static Boolean MainHandleEvent( EventPtr );
- static Boolean MenuApplicationEvent( EventPtr );
- static void StopApplication( void );
-
- static void UpdateAllTimers( void );
- static void ToggleTimerRun( Int );
- static void StartTimer( Int );
- static void StopTimer( Int );
- static void UpdateTimer( Int, Boolean );
- static void SetTimeFldEditable( Int, Boolean );
- static void GetUsersTime ( Int );
- static void FormatTime ( Long, char * );
- static void FormatDate ( char * );
- static Boolean TxtFieldHasFocus( Boolean );
- static Word GetFocusID( void );
- static void UpdateTotalTime( Boolean );
-
- /*************************************************************
- *
- * NAME: PilotMain
- *
- * DESCRIPTION: This is the main entry point for the
- * application.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- -------- -----------
- * kld 5 Nov 98 Initial Revision
- * kld 6 Feb 99 Add setting of bold flag for font
- * of running timer. For some reason the
- * bold font causes a "Fatal Exception" on
- * PalmOS 2.0 running on actual hardware but
- * not on PalmOS 2.0 debug ROM running on an
- * emulator, so disable it on PalmOS 2.0
- * kld 17 Feb 99 Moved bold flag to StartApplication
- *
- *************************************************************/
- DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags )
- {
- short err;
- Word error;
- Int i;
-
- CALLBACK_PROLOGUE
-
- err = CheckRomVersion( Version20, launchFlags );
- if (err) return (err);
-
- switch (cmd)
- {
- case sysAppLaunchCmdNormalLaunch:
- error = StartApplication ();
- if (error) return (error);
-
- FrmGotoForm( tcloggerFormMain );
-
- EventLoop();
- StopApplication();
- break;
-
- default:
- break;
- }
-
- CALLBACK_EPILOGUE
- return 0;
- }
-
-
- /*************************************************************
- *
- * NAME: StartApplication
- *
- * DESCRIPTION: This routine loads the saved prefrences
- * information and initializes global variables.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- -------- -----------
- * kld 5 Nov 98 Initial Revision
- * kld 17 Feb 99 Moved bold flag from PilotMain to here
- *
- *************************************************************/
- static Word StartApplication( void )
- {
- Word error = 1;
- TCLoggerPrefType prefs;
- Int prefsVersion;
- Int prefsSize;
- Int i;
- DWord romVersion;
-
- /* Get the ROM version for which PalmOS version is being used. */
- FtrGet( sysFtrCreator, sysFtrNumROMVersion, &romVersion );
- bOKBold = (romVersion >= Version30);
-
- /* Get system preferences for In/Out date/time format */
- PrefGetPreferences( &sysPrefs );
-
- /* Read the preferences saved-state information. */
- prefsSize = sizeof (TCLoggerPrefType);
- prefsVersion = PrefGetAppPreferences (tcloggerCreator,
- TCLoggerPrefID,
- &prefs,
- &prefsSize,
- true);
- if (prefsVersion == TCLoggerPrefsVersionNum)
- {
- AccumFormat = prefs.AccumFormat;
- StrCopy( sInTime, prefs.sInTime );
- StrCopy( sOutTime, prefs.sOutTime );
- for (i=0; i<NUMTIMERS; i++)
- TimerData[i] = prefs.TimerState[i];
-
- error = 0;
- }
- /* Set to defaults if no preferences found */
- else if (prefsVersion == noPreferenceFound)
- {
- AccumFormat = HOURS;
- StrCopy( sInTime, "None" );
- StrCopy( sOutTime, "None" );
- for (i=0; i<NUMTIMERS; i++)
- {
- TimerData[i].bRunning = false;
- TimerData[i].lStartTime = 0L;
- TimerData[i].lAccumTime = 0L;
- StrCopy( TimerData[i].sDescription,"" );
- }
- error = 0;
- }
-
- hndExport = MemHandleNew( 400 );
- if (hndExport == NULL) return (1);
-
- hndInDesc = MemHandleNew( 41 );
- if (hndInDesc == NULL) return (1);
- hndOutDesc = MemHandleNew( 41 );
- if (hndOutDesc == NULL) return (1);
-
- for (i=0; i<NUMTIMERS; i++)
- {
- hndTime[i] = MemHandleNew( 7 );
- if (hndTime[i] == NULL) return (1);
- hndDesc[i] = MemHandleNew( 21 );
- if (hndDesc[i] == NULL) return (1);
- }
-
- hndTotal = MemHandleNew( 7 );
- if (hndTotal == NULL) return (1);
-
- return (error);
- }
-
-
- /*************************************************************
- *
- * NAME: EventLoop
- *
- * DESCRIPTION: This routine is the event loop for processing
- * events.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 29 Nov 98 Initial Revision
- *
- *************************************************************/
- static void EventLoop( void )
- {
- Word error;
- EventType event;
-
- do
- {
- // Get the next available event.
- EvtGetEvent( &event, evtWaitForever );
-
- // Give the system a chance to handle the event.
- if ( ! SysHandleEvent( &event ) )
-
- // Give the menu bar a chance to update and handle the event.
- if ( ! MenuHandleEvent( (void *) 0, &event, &error ) )
-
- // Give the application a chance to handle the event.
- if ( ! ApplicationHandleEvent( &event ) )
-
- // Let the form object provide default handling of the event.
- FrmDispatchEvent( &event );
- }
-
- while (event.eType != appStopEvent);
- }
-
-
- /*************************************************************
- *
- * NAME: EventLoop
- *
- * DESCRIPTION: This routine is the event loop for processing
- * events.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 29 Nov 98 Initial Revision
- * kld 6 Feb 99 Remove bold font for OS 2.0
- * kld 17 Feb 99 Add call to update total time.
- *
- *************************************************************/
- static Boolean ApplicationHandleEvent( EventPtr pEvent )
- {
- FormPtr pFrm;
- Int idForm;
- Boolean bHandled = false;
- CharPtr sBuff;
- FieldPtr pFld;
- Int i;
- ControlPtr pCtl;
- Char sID[ 10 ];
-
- if (pEvent->eType == frmLoadEvent)
- {
- // Load the form resource specified in the event then activate the form.
- idForm = pEvent->data.frmLoad.formID;
- pFrm = FrmInitForm( idForm );
- FrmSetActiveForm( pFrm );
-
- /* Set the running checkbox */
- for (i=0; i<NUMTIMERS; i++)
- {
- /* Set the Run checkbox */
- pCtl = (ControlPtr) Id2Ptr( idRunCB+i );
- CtlSetValue (pCtl, TimerData[i].bRunning);
- SetTimeFldEditable( i, !TimerData[i].bRunning );
- /* Set the timers description */
- pFld = Id2Ptr( idDescFld+i );
- sBuff = MemHandleLock( hndDesc[i] );
- StrCopy( sBuff, TimerData[i].sDescription );
- MemHandleUnlock( hndDesc[i] );
- FldSetTextHandle( pFld, (Handle) hndDesc[i] );
- /* If running highlite description font and
- update the time accumulated */
- if (TimerData[i].bRunning && bOKBold)
- FldSetFont( pFld, boldFont );
- UpdateTimer( i, false );
- }
- UpdateTotalTime( false );
-
- /* Set the clock in time */
- pFld = Id2Ptr( idInFld );
- sBuff = MemHandleLock( hndInDesc );
- StrCopy( sBuff, sInTime );
- MemHandleUnlock( hndInDesc );
- FldSetTextHandle ( pFld, (Handle) hndInDesc );
-
- /* Set the clock out time */
- pFld = Id2Ptr( idOutFld );
- sBuff = MemHandleLock( hndOutDesc );
- StrCopy( sBuff, sOutTime );
- MemHandleUnlock( hndOutDesc );
- FldSetTextHandle ( pFld, (Handle) hndOutDesc );
-
- // Set the event handler for the form. The handler of the currently
- // active form is called by FrmDispatchEvent each time it receives an event.
- switch (idForm)
- {
- case tcloggerFormMain:
- FrmSetEventHandler( pFrm, MainHandleEvent );
- break;
-
- default:
- StrIToA( sID, idForm );
- FrmCustomAlert( UnknownFormAlert, sID, NULL, NULL );
- break;
- }
- bHandled = true;
- }
-
- return bHandled;
- }
-
-
- /*************************************************************
- *
- * NAME: MainHandleEvent
- *
- * DESCRIPTION: This routine processes events for the main
- * form.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 29 Nov 98 Initial Revision
- *
- *************************************************************/
- static Boolean MainHandleEvent( EventPtr pEvent )
- {
- Boolean bHandled = false;
- FormPtr pFrm;
- CharPtr sBuff;
- FieldPtr pFld;
- Int i;
- ControlPtr pCtl;
-
- CALLBACK_PROLOGUE
-
- switch (pEvent->eType)
- {
- case ctlSelectEvent:
- if (pEvent->data.ctlSelect.controlID == idUpdateB)
- {
- UpdateAllTimers();
- bHandled = true;
- }
- else if((pEvent->data.ctlSelect.controlID >= idRunCB) &&
- (pEvent->data.ctlSelect.controlID < (idRunCB+NUMTIMERS)))
- {
- ToggleTimerRun( pEvent->data.ctlSelect.controlID - idRunCB );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idInB)
- {
- FormatDate( sInTime );
-
- pFld = Id2Ptr( idInFld );
- sBuff = (CharPtr) MemHandleLock( hndInDesc );
- StrCopy( sBuff, sInTime );
- MemHandleUnlock( hndInDesc );
- FldSetTextHandle ( pFld, (Handle) hndInDesc );
- FldDrawField( pFld );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idOutB)
- {
- FormatDate( sOutTime );
-
- pFld = Id2Ptr( idOutFld );
- sBuff = (CharPtr) MemHandleLock( hndOutDesc );
- StrCopy( sBuff, sOutTime );
- MemHandleUnlock( hndOutDesc );
- FldSetTextHandle ( pFld, (Handle) hndOutDesc );
- FldDrawField( pFld );
- bHandled = true;
- }
-
- case frmOpenEvent:
- pFrm = FrmGetActiveForm();
-
- FrmDrawForm( pFrm );
-
- bHandled = true;
- break;
-
- case keyDownEvent:
- /* Hard button pushed that did not power on the pilot */
- if (ChrIsHardKey( pEvent->data.keyDown.chr ))
- {
- if (! (pEvent->data.keyDown.modifiers & poweredOnKeyMask))
- {
- /* Launch the ToDo list application.
- Done so that TCLogger could be mapped to ToDo's hardware
- button. Pressing ToDo's hardware button with TCLogger
- running will launch ToDo Application */
- Err err; // Why is this var needed? See <AppLaunchCmd.h>!
- AppLaunchWithCommand(sysFileCToDo,
- sysAppLaunchCmdNormalLaunch,
- NULL);
- bHandled = true;
- }
- else
- {
- UpdateAllTimers();
- bHandled = true;
- }
- }
-
- case menuEvent:
- bHandled = MenuApplicationEvent( pEvent );
- break;
-
- default:
- bHandled = false;
- }
-
- CALLBACK_EPILOGUE
-
- return bHandled;
- }
-
-
- /*************************************************************
- *
- * NAME: MenuApplicationEvent
- *
- * DESCRIPTION: This routine processes menu events for the
- * main form.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 4 Dec 98 Initial Revision
- * kld 8 Feb 99 Add check for edited time before export.
- * kld 15 Feb 99 Added total accumulated time to
- * exported data.
- * kld 17 Feb 99 Add call to update total time.
- *
- *************************************************************/
- static Boolean MenuApplicationEvent( EventPtr pEvent )
- {
- Boolean bHandled = false;
- CharPtr sFld;
- CharPtr sBuff;
- Char sTime[10];
- Char sDate[20];
- DateTimeType dtNow;
- FieldPtr pFld;
- Int i;
- Word wFocused;
- Int iTimer;
- Long lTotalTime = 0L;
- Long lRound = 1L;
-
- if (pEvent->data.ctlSelect.controlID == idExportMenu)
- {
- sFld = (CharPtr) MemHandleLock( hndExport );
- StrCopy( sFld, "TCLogger Time " );
- TimSecondsToDateTime( TimGetSeconds(), &dtNow );
- DateToAscii( dtNow.month, dtNow.day, dtNow.year, sysPrefs.longDateFormat, sDate );
- StrCat( sFld, sDate );
- StrCat( sFld, "\n\n" );
-
- /* Export Clock In Time */
- sBuff = (CharPtr) MemHandleLock( hndInDesc );
- StrCat( sFld, "In Time:\t\t" );
- StrCat( sFld, sBuff );
- StrCat( sFld, "\n\n" );
- MemHandleUnlock( hndInDesc );
-
- if (AccumFormat == HOURS)
- lRound = 360L;
- else if (AccumFormat == HOURS_MIN)
- lRound = 60L;
-
- /* Export All the non-zero timers and descriptions */
- for (i=0; i<NUMTIMERS; i++)
- {
- if (FldDirty ( Id2Ptr( idTimeFld+i) ))
- GetUsersTime( i );
- if (! TimerData[i].bRunning &&
- ( TimerData[i].lAccumTime > 0L ) )
- {
- /* Add to total, rounding is done so that fractional
- parts do not add up and cause total to appear
- too large */
- lTotalTime = lTotalTime + TimerData[i].lAccumTime -
- (TimerData[i].lAccumTime%lRound);
- FormatTime( TimerData[i].lAccumTime, sTime );
- StrCat( sFld, sTime );
- StrCat( sFld, "\t" );
- /* Get the current description */
- pFld = Id2Ptr( idDescFld+i );
- sBuff = MemHandleLock( hndDesc[i] );
- StrCopy( TimerData[i].sDescription, sBuff );
- MemHandleUnlock( hndDesc[i] );
- StrCat( sFld, TimerData[i].sDescription );
- StrCat( sFld, "\n" );
- }
- }
-
- /* Export the total Time Charged */
- FormatTime( lTotalTime, sTime );
- StrCat( sFld, sTime );
- StrCat( sFld, "\tTotal Charged\n" );
-
- /* Export Clock Out Time */
- sBuff = (CharPtr) MemHandleLock( hndOutDesc );
- StrCat( sFld, "\nOut Time:\t" );
- StrCat( sFld, sBuff );
- StrCat( sFld, "\n" );
- MemHandleUnlock( hndOutDesc );
-
- MemHandleUnlock( hndExport );
- ExportToMemoPad( hndExport );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idClearFocusMenu)
- {
- if ( TxtFieldHasFocus( false ) )
- {
- wFocused = FrmGetFocus( FrmGetActiveForm() );
- wFocused = FrmGetObjectId( FrmGetActiveForm(), wFocused );
- if ( wFocused >= idTimeFld &&
- wFocused < ( idTimeFld + NUMTIMERS ) )
- {
- iTimer = wFocused - idTimeFld;
- }
- else if ( wFocused >= idDescFld &&
- wFocused < ( idDescFld + NUMTIMERS ) )
- {
- iTimer = wFocused - idDescFld;
- }
-
- TimerData[iTimer].lStartTime = 0L;
- TimerData[iTimer].lAccumTime = 0L;
- UpdateTimer( iTimer, true );
- UpdateTotalTime( true );
- }
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idClearAllMenu)
- {
- for (i=0; i<NUMTIMERS; i++)
- {
- if (! TimerData[i].bRunning )
- {
- TimerData[i].lStartTime = 0L;
- TimerData[i].lAccumTime = 0L;
- UpdateTimer( i, true );
- }
- }
- UpdateTotalTime( true );
-
- StrCopy( sInTime, "None" );
- pFld = Id2Ptr( idInFld );
- sBuff = MemHandleLock( hndInDesc );
- StrCopy( sBuff, sInTime );
- MemHandleUnlock( hndInDesc );
- FldSetTextHandle ( pFld, (Handle) hndInDesc );
- FldDrawField( pFld );
-
- StrCopy( sOutTime, "None" );
- pFld = Id2Ptr( idOutFld );
- sBuff = MemHandleLock( hndOutDesc );
- StrCopy( sBuff, sOutTime );
- MemHandleUnlock( hndOutDesc );
- FldSetTextHandle ( pFld, (Handle) hndOutDesc );
- FldDrawField( pFld );
-
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idClearIOMenu)
- {
- StrCopy( sInTime, "None" );
- pFld = Id2Ptr( idInFld );
- sBuff = MemHandleLock( hndInDesc );
- StrCopy( sBuff, sInTime );
- MemHandleUnlock( hndInDesc );
- FldSetTextHandle ( pFld, (Handle) hndInDesc );
- FldDrawField( pFld );
-
- StrCopy( sOutTime, "None" );
- pFld = Id2Ptr( idOutFld );
- sBuff = MemHandleLock( hndOutDesc );
- StrCopy( sBuff, sOutTime );
- MemHandleUnlock( hndOutDesc );
- FldSetTextHandle ( pFld, (Handle) hndOutDesc );
- FldDrawField( pFld );
-
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idHoursMenu)
- {
- AccumFormat = HOURS;
- UpdateAllTimers();
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idHrMinMenu)
- {
- AccumFormat = HOURS_MIN;
- UpdateAllTimers();
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idSecMenu)
- {
- AccumFormat = SECONDS;
- UpdateAllTimers();
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idHelpMenu)
- {
- FrmHelp( tcloggerHelp1 );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idAboutMenu)
- {
- FrmHelp( tcloggerAbout1 );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idKeyBrdMenu)
- {
- /* See Keyboard.h for details */
- SysKeyboardDialog( kbdDefault );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idGHelpMenu)
- {
- /* See GraffitiReference.h for details */
- SysGraffitiReferenceDialog( referenceDefault );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idUndoMenu)
- {
- if ( TxtFieldHasFocus( true ) )
- FldUndo( Id2Ptr( GetFocusID() ) );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idCutMenu)
- {
- if ( TxtFieldHasFocus( true ) )
- FldCut( Id2Ptr( GetFocusID() ) );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idCopyMenu)
- {
- if ( TxtFieldHasFocus( true ) )
- FldCopy( Id2Ptr( GetFocusID() ) );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idPasteMenu)
- {
- if ( TxtFieldHasFocus( true ) )
- FldPaste( Id2Ptr( GetFocusID() ) );
- bHandled = true;
- }
- else if (pEvent->data.ctlSelect.controlID == idSelectMenu)
- {
- if ( TxtFieldHasFocus( true ) )
- FldSetSelection(Id2Ptr( GetFocusID() ),
- 0,
- FldGetTextLength( Id2Ptr( GetFocusID() ) ));
- bHandled = true;
- }
- return bHandled;
- }
-
-
- /*************************************************************
- *
- * NAME: StopApplication
- *
- * DESCRIPTION: This routine saves the current state
- * of the application.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 5 Nov 98 Initial Revision
- * kld 29 Nov 98 Change event loop processing
- *
- *************************************************************/
- static void StopApplication( void )
- {
- TCLoggerPrefType prefs;
- Int i;
- CharPtr sDesc;
-
- prefs.AccumFormat = AccumFormat;
-
- sDesc = (CharPtr) MemHandleLock( hndInDesc );
- StrCopy( prefs.sInTime, sDesc );
- MemHandleUnlock( hndInDesc );
-
- sDesc = (CharPtr) MemHandleLock( hndOutDesc );
- StrCopy( prefs.sOutTime, sDesc );
- MemHandleUnlock( hndOutDesc );
-
- for (i=0; i<NUMTIMERS; i++)
- {
- sDesc = MemHandleLock( hndDesc[i] );
- StrCopy( TimerData[i].sDescription, sDesc );
- MemHandleUnlock( hndDesc[i] );
- if (FldDirty ( Id2Ptr( idTimeFld+i ) ))
- GetUsersTime( i );
-
- prefs.TimerState[i] = TimerData[i];
- }
-
- PrefSetAppPreferences (tcloggerCreator,
- TCLoggerPrefID,
- TCLoggerPrefsVersionNum,
- &prefs,
- sizeof (TCLoggerPrefType),
- true);
-
- /* FrmCloseAllForms will send all opened forms a
- frmCloseEvent. */
- FrmCloseAllForms();
- }
-
-
- /*************************************************************
- *
- * NAME: ToggleTimerRun
- *
- * DESCRIPTION: This routine saves the current state
- * of the application.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 1 Dec 98 Initial Development
- * kld 17 Feb 99 Add call to update total time.
- *
- *************************************************************/
- static void ToggleTimerRun( Int iTimer )
- {
- if (TimerData[iTimer].bRunning)
- StopTimer( iTimer );
- else
- StartTimer( iTimer );
- TimerData[iTimer].bRunning = !TimerData[iTimer].bRunning;
- SetTimeFldEditable( iTimer, !TimerData[iTimer].bRunning );
-
- UpdateTotalTime( true );
- }
-
-
- /*************************************************************
- *
- * NAME: StartTimer
- *
- * DESCRIPTION: This routine starts the given timer. If
- * the user has changed the accumulated time,
- * then it is read in and used (if the format
- * is valid).
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 1 Dec 98 Initial Development
- * kld 6 Feb 99 Remove bold font for OS 2.0
- *
- *************************************************************/
- static void StartTimer( Int iTimer )
- {
- FieldPtr pFld = Id2Ptr( idDescFld+iTimer);
-
- if (FldDirty ( Id2Ptr( idTimeFld+iTimer) ))
- {
- GetUsersTime( iTimer );
- }
- TimerData[iTimer].lStartTime = TimGetSeconds();
- if (bOKBold)
- FldSetFont( pFld, boldFont );
- FldDrawField( pFld );
- UpdateTimer( iTimer, true );
- }
-
-
- /*************************************************************
- *
- * NAME: StopTimer
- *
- * DESCRIPTION: This routine stops the given timer from
- * running.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 1 Dec 98 Initial Development
- *
- *************************************************************/
- static void StopTimer( Int iTimer )
- {
- FieldPtr pFld = Id2Ptr( idDescFld+iTimer);
-
- FldSetFont( pFld, stdFont );
- FldDrawField( pFld );
- UpdateTimer( iTimer, true );
- }
-
-
- /*************************************************************
- *
- * NAME: UpdateTimer
- *
- * DESCRIPTION: This routine updates the displayed time.
- * If the timer is running it just displays
- * what the current accumulated time is without
- * updating global timers.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 1 Dec 98 Initial Development
- *
- *************************************************************/
- static void UpdateTimer( Int iTimer, Boolean bUpdateDisplay )
- {
- Long lTime = 0L;
- FieldPtr pFld;
- CharPtr sFld;
-
- if ( TimerData[iTimer].bRunning )
- {
- lTime = TimGetSeconds();
- TimerData[iTimer].lAccumTime = TimerData[iTimer].lAccumTime +
- (lTime - TimerData[iTimer].lStartTime);
- TimerData[iTimer].lStartTime = lTime;
- }
- else if (FldDirty ( Id2Ptr( idTimeFld+iTimer) ))
- GetUsersTime( iTimer );
- lTime = TimerData[iTimer].lAccumTime;
-
- pFld = Id2Ptr(idTimeFld+iTimer);
- sFld = (CharPtr) MemHandleLock( hndTime[iTimer] );
-
- FormatTime( lTime, sFld );
-
- MemHandleUnlock( hndTime[iTimer] );
- FldSetTextHandle( pFld, (Handle) hndTime[iTimer] );
- if ( bUpdateDisplay )
- FldDrawField( pFld );
- }
-
-
- /*************************************************************
- *
- * NAME: UpdateAllTimers
- *
- * DESCRIPTION: This routine updates all timers accumulated
- * time.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 1 Dec 98 Initial Development
- * kld 17 Feb 99 Add call to update total time.
- *
- *************************************************************/
- static void UpdateAllTimers( void )
- {
- Int i;
-
- for (i=0; i<NUMTIMERS; i++)
- {
- UpdateTimer( i, true );
- }
- UpdateTotalTime( true );
- }
-
-
- /*************************************************************
- *
- * NAME: SetTimeFldEditable
- *
- * DESCRIPTION: This routine will mark a time field as editable
- * based on the passed in value.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 2 Dec 98 Initial development
- *
- *************************************************************/
- static void SetTimeFldEditable( Int iTimer, Boolean bEdit )
- {
- FieldPtr pTimeFld = Id2Ptr( idTimeFld + iTimer );
- FieldAttrType attr;
-
- FldGetAttributes( pTimeFld, &attr );
- attr.editable = bEdit;
- FldSetAttributes( pTimeFld, &attr );
- FldReleaseFocus( pTimeFld );
- }
-
-
- /*************************************************************
- *
- * NAME: GetUsersTime
- *
- * DESCRIPTION: This routine will read in the user entered
- * time in the format H:M, H.H or SEC. If an error
- * occurs the last saved accumulated time will be
- * restored in the time field.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 2 Dec 98 Initial development
- *
- *************************************************************/
- static void GetUsersTime ( Int iTimer )
- {
- Boolean bError = false;
- Boolean bHoursOnly = false;
- Boolean bMinutes = false;
- Int iDecimal = 1;
- Long lCurrent = 0L;
- Long lTime = 0L;
- CharPtr pTime = FldGetTextPtr( Id2Ptr( idTimeFld + iTimer ) );
- Int length = StrLen( pTime );
- Int i;
- Char sBuff[3];
-
- if (pTime)
- {
- for (i=0; i<length; i++)
- {
- if ( *pTime >= '0' && *pTime <= '9' )
- {
- lCurrent = ( lCurrent * 10L ) + ( *pTime - '0' );
- if (bHoursOnly)
- iDecimal*=10;
- }
- else if (*pTime == '.' && (! bHoursOnly) && (! bMinutes))
- {
- bHoursOnly = true;
- }
- else if (*pTime == ':' && (! bHoursOnly) && (! bMinutes))
- {
- bMinutes = true;
- /* Convert hours to minutes */
- lTime = lCurrent * 60L;
- lCurrent = 0L;
- }
- else if (*pTime == ' ')
- /* Skip whitespace */
- ;
- else
- bError = true;
- pTime++;
- }
- /* If there is not an error, the users time will be used
- if there is an error the old accumulated time will be
- used. */
- if (!bError)
- {
- if ( bHoursOnly )
- /* Convert hours to seconds */
- lTime = ((lCurrent * 3600L) / (Long) (iDecimal));
- else if ( bMinutes )
- /* Convert minutes to seconds */
- lTime = (lTime + lCurrent) * 60L;
- else
- lTime = lCurrent;
- TimerData[ iTimer ].lAccumTime = lTime;
- }
- else
- {
- StrIToA( sBuff, iTimer+1 );
- FrmCustomAlert( TimeParseAlert, sBuff, NULL, NULL );
- }
- }
- }
-
- /*************************************************************
- *
- * NAME: ICat
- *
- * DESCRIPTION: This routine Cat the given integer to the
- * given string
- *
- *************************************************************/
- void ICat( UInt ui, char *s )
- {
- Char sBuff[ 10 ];
-
- StrIToA( sBuff, ui );
- StrCat( s, sBuff );
- }
-
- /*************************************************************
- *
- * NAME: FormatTime
- *
- * DESCRIPTION: This routine Cat the given integer to the
- * given string and will pad the 10s place with
- * 0 if needed.
- *
- *************************************************************/
- static void ZeroPadICat( UInt ui, char *s )
- {
- if ( ui < 10 )
- {
- StrCat( s, "0" );
- }
-
- ICat( ui, s );
- }
-
- /*************************************************************
- *
- * NAME: FormatTime
- *
- * DESCRIPTION: This routine will format the given time in
- * the selected format of H.H, H:M, SEC
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 3 Dec 98 Initial development
- *
- *************************************************************/
- static void FormatTime( Long lTime, char *sTime )
- {
- UInt uiHours, uiMin, uiFrac;
-
- if (AccumFormat == HOURS)
- {
- uiHours = (UInt) (lTime / 3600L );
- lTime = (Long) ( lTime % 3600L );
- uiFrac = (UInt) (lTime / 360L );
-
- StrCopy ( sTime, "" );
- ICat( uiHours, sTime );
- StrCat( sTime, "." );
- ICat( uiFrac, sTime );
- }
- else if (AccumFormat == HOURS_MIN)
- {
- uiHours = (UInt) (lTime / 3600L );
- lTime = (Long) ( lTime % 3600L );
- uiMin = (UInt) (lTime / 60L );
-
- StrCopy ( sTime, "" );
- ICat( uiHours, sTime );
- StrCat( sTime, ":" );
- ZeroPadICat( uiMin, sTime );
- }
- else
- {
- StrIToA( sTime, (Int) lTime);
- }
- }
-
-
- /*************************************************************
- *
- * NAME: FormatDate
- *
- * DESCRIPTION: This routine will format the given time for
- * use with the clock in/out function.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 3 Dec 98 Initial development
- *
- *************************************************************/
- static void FormatDate ( char *sDesc )
- {
- CharPtr ptrEOS;
- DateTimeType dtNow;
-
- TimSecondsToDateTime( TimGetSeconds(), &dtNow );
-
- DateToAscii( dtNow.month, dtNow.day, dtNow.year, sysPrefs.dateFormat, sDesc );
- StrCat( sDesc, " " );
- ptrEOS = sDesc + StrLen( sDesc );
- TimeToAscii( dtNow.hour, dtNow.minute, sysPrefs.timeFormat, ptrEOS );
- }
-
-
- /*************************************************************
- *
- * NAME: TxtFieldHasFocus
- *
- * DESCRIPTION: This routine will determine if a text field
- * currently has focus.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 3 Dec 98 Initial development
- *
- *************************************************************/
- static Boolean TxtFieldHasFocus( Boolean bCheckInOut )
- {
- Word wFocused = FrmGetFocus( FrmGetActiveForm() );
- Boolean bIsTxtFld = 0;
-
- if ( wFocused != -1 )
- {
- wFocused = FrmGetObjectId( FrmGetActiveForm(), wFocused );
- bIsTxtFld = ( ( wFocused >= idTimeFld &&
- wFocused < ( idTimeFld + NUMTIMERS ) ) ||
- ( wFocused >= idDescFld &&
- wFocused < ( idDescFld + NUMTIMERS ) ) ||
- ( bCheckInOut && ( wFocused == idInFld ||
- wFocused == idOutFld ) ) );
- }
-
- if ( !bIsTxtFld )
- {
- SndPlaySystemSound( sndError );
- FrmAlert( NoFieldSelectAlert );
- }
-
- return( bIsTxtFld );
- }
-
-
- /*************************************************************
- *
- * NAME: GetFocusID
- *
- * DESCRIPTION: This routine gets the id of the frame object
- * that currently had data entry focus.
- *
- *************************************************************/
- static Word GetFocusID( void )
- {
- Word wFocused = FrmGetFocus( FrmGetActiveForm() );
-
- if ( wFocused != -1 )
- {
- wFocused = FrmGetObjectId( FrmGetActiveForm(), wFocused );
- }
-
- return( wFocused );
- }
-
-
- /*************************************************************
- *
- * NAME: UpdateTotalTime
- *
- * DESCRIPTION: This routine will update the total time
- * displayed.
- *
- * REVISION HISTORY:
- * Name Date Description
- * ---- --------- -----------
- * kld 17 Feb 99 Initial development
- *
- *************************************************************/
- static void UpdateTotalTime( Boolean bUpdateDisplay )
- {
- CharPtr sFld;
- FieldPtr pFld;
- Long lTotalTime = 0L;
- Long lRound = 1L;
- Int i;
-
- if (AccumFormat == HOURS)
- lRound = 360L;
- else if (AccumFormat == HOURS_MIN)
- lRound = 60L;
-
- for (i=0; i<NUMTIMERS; i++)
- {
- if (FldDirty ( Id2Ptr( idTimeFld+i) ))
- GetUsersTime( i );
- if ( TimerData[i].lAccumTime > 0L )
- /* Add to total, rounding is done so that fractional
- parts do not add up and cause total to appear
- too large */
- lTotalTime = lTotalTime + TimerData[i].lAccumTime -
- (TimerData[i].lAccumTime%lRound);
- }
-
- pFld = Id2Ptr( idTotalFld );
- sFld = (CharPtr) MemHandleLock( hndTotal );
-
- FormatTime( lTotalTime, sFld );
-
- MemHandleUnlock( hndTotal );
- FldSetTextHandle( pFld, (Handle) hndTotal );
-
- if ( bUpdateDisplay )
- FldDrawField( pFld );
- }
-